﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;
namespace MockPrjTeam2.Pages.Views
{
    public partial class ListOrganizations : System.Web.UI.Page
    {
        OrganizationBuz organizationBus = new OrganizationBuz();
        private static int flag;
        private static int check;
        private static string key=string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                flag = 0;
                check = 0;
                LoadAllOrganizations();
               
            }
           
        }

        private void LoadAllOrganizations()
        {
            GridViewOrg.DataSource = organizationBus.GetAllOrganizations();
            //GridViewOrg.PageIndex = 0;
            GridViewOrg.DataBind();

        }
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            if (CheckBoxInactive.Checked)
            {
                key = e.CommandName;
                check = 1;
                flag = 1;
                GridViewOrg.DataSource = organizationBus.FilterOrganizationByLetters(key, flag);
              
                GridViewOrg.DataBind();
            }
            else
            {
                key = e.CommandName;
                check = 1;
                flag = 0;
                GridViewOrg.DataSource = organizationBus.FilterOrganizationByLetters(key, flag);
               
                GridViewOrg.DataBind();
            }
        }

        protected void LinkButtonOrganisationName_Command(object sender, CommandEventArgs e)
        {

            
             Response.Redirect(string.Format(CultureInfo.CurrentCulture, "../Forms/OrganizationDetails.aspx?id={0}", e.CommandName));
        }

        protected void CheckBoxInactiveCheckedChanged(object sender, EventArgs e)
        {

            if (check == 1)
            {
                 
                    if (CheckBoxInactive.Checked && check == 1)
                    {
                        flag = 1;
                        GridViewOrg.DataSource = organizationBus.FilterOrganizationByLetters(key, flag);
                  
                       GridViewOrg.DataBind();

                    }
                
                else if (CheckBoxInactive.Checked==false&&check==1)
                {

                    flag = 0;
                    GridViewOrg.DataSource = organizationBus.FilterOrganizationByLetters(key, flag);
                  
                    GridViewOrg.DataBind();

                }
                    
            }
            else if(check==0)
            {

                 if (CheckBoxInactive.Checked&&check==0)
                {
                    GridViewOrg.DataSource = organizationBus.GetOrganizationsInActive();
                  
                     GridViewOrg.DataBind();
                
                }

                else
                 {
                     flag = 0;
                    LoadAllOrganizations();
                }

                

        }




    }

       
        protected void LbnOrganizationName_Command(object sender, CommandEventArgs e)
        {
         
            if (e.CommandArgument.Equals("False"))
            {
                int organisationId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                bool result = organizationBus.SetActiveOrganization(organisationId, true);
            }

            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "../Forms/OrganizationDetails.aspx?id={0}",
                                            e.CommandName));
        }

        protected void GridViewOrg_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewOrg.PageIndex =e.NewPageIndex;
            if (check == 1)
            {
                if (CheckBoxInactive.Checked&&check==1)
                {
                    flag = 1;
                    GridViewOrg.DataSource = organizationBus.FilterOrganizationByLetters(key, flag);
                }
                else if(CheckBoxInactive.Checked==false&&check==1)
                {
                    flag = 0;
                    GridViewOrg.DataSource = organizationBus.FilterOrganizationByLetters(key, flag);
                }
            }
            else if(check==0)
            {
                if(CheckBoxInactive.Checked&&check==0)
                {
                    GridViewOrg.DataSource = organizationBus.GetOrganizationsInActive();
                }
                else
                {
                    flag = 0;
                    GridViewOrg.DataSource = organizationBus.GetAllOrganizations();
                }
            
            }
          
            GridViewOrg.DataBind();
          
            
        }

        protected void GridViewOrg_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            }
        }

     



    }
}